<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getUserList">
            <el-button slot="append" icon="el-icon-search" @click="getUserList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="showAddDialog = true">添加用户</el-button>
        </el-col>
      </el-row>
      <el-table :data="userList" border stripe style="width: 100%">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="username" label="用户名" width="180"></el-table-column>
        <el-table-column prop="email" label="邮箱" width="180"></el-table-column>
        <el-table-column prop="mobile" label="电话"></el-table-column>
        <el-table-column prop="role_name" label="角色"></el-table-column>
        <el-table-column prop="mg_state" label="状态">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.mg_state" active-color="#13ce66" inactive-color="#ff4949" @change="handleSwitch(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-tooltip effect="dark" content="修改用户" placement="top">
              <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="删除用户" placement="top">
              <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteUserById(scope.row.id)"></el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="用户设置" placement="top">
              <el-button type="warning" icon="el-icon-setting" size="mini"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
      <add-dialog :isVisible="showAddDialog" @cancel="handleAddDialogCancel" @commit="handleAddDialogCommit"></add-dialog>
    </el-card>
  </div>
</template>

<script>
import { getUserListRequest, changeUserStateRequest, createUserRequest, deleteUserRequest } from '../../api/request'
import AddDialog from './AddDialog'
export default {
  name: 'User',
  components: {
    'add-dialog': AddDialog
  },
  data() {
    return {
      queryInfo: {
        query: '',
        pagesize: 2,
        pagenum: 1
      },
      userList: [],
      total: 0,
      showAddDialog: false
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    getUserList() {
      getUserListRequest(this.queryInfo).then(res => {
        if (res.meta.status !== 200) return this.$message.error('获取用户信息失败')
        this.userList = res.data.users
        this.total = res.data.total
      })
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getUserList()
    },
    handleCurrentChange(newPageNum) {
      this.queryInfo.pagenum = newPageNum
      this.getUserList()
    },
    handleSwitch(userInfo) {
      changeUserStateRequest(userInfo.id, !userInfo.mg_state).then(res => {
        console.log(res);
        if (res.meta.status !== 201) {
          userInfo.mg_state = !userInfo.mg_state
          return this.$message.error('更新用户状态信息失败')
        }
        this.$message.success('更新用户状态信息成功')
      })
    },
    handleAddDialogCancel() {
      this.showAddDialog = !this.showAddDialog
    },
    handleAddDialogCommit(data, form) {
      const payload = {
        username: data.username,
        email: data.email,
        mobile: data.mobile,
        password: data.password
      }
      createUserRequest(payload).then(res => {
        this.showAddDialog = false
        if (res.meta.status !== 201) {
          return this.$message.error('新建用户失败!')
        }
        form.resetFields()
        this.$message.success('新建用户成功!')
        this.getUserList()
      })
    },
    deleteUserById(id) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteUserRequest(id).then(res => {
            if (res.meta.status !== 200) return this.$message.error('删除用户失败！')
            this.$message.success('删除用户成功')
            this.getUserList()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style lang="less" scoped>

</style>